package com.taxshare.seller.util.sst;

import java.util.*;

import com.taxshare.seller.constant.CodeConstant;
import com.taxshare.tools.util.DateUtil;
import groovyjarjarpicocli.CommandLine;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.json.JSONObject;

import com.taxshare.seller.entity.SellerInvoiceDetailEntity;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.tools.util.StringUtil;
import com.taxshare.tools.util.UUIDUitl;

@Slf4j
public class IntJsonUtil {

    static final String TRUE = "true";
    static final String FPLXDM = "fplxdm";

    /**
     * 空白发票同步
     *
     * @param appid
     * @param fplxdm
     * @return
     */
    public static String blankInvoice(String appid, String nsrsbh, String fplxdm) {
        Map map = new HashMap();
        map.put(FPLXDM, fplxdm);
        return getJson("GPXXCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(false), map);

    }

    /**
     * 通用头
     *
     * @param serviceId
     * @param nsrsbh
     * @param async
     * @param appid
     * @param sid
     * @param inpuMap
     * @return
     */
    public static String getJson(String serviceId, String nsrsbh, String async, String appid, String sid, Map inpuMap) {
        JSONObject json = new JSONObject();
        json.put("serviceid", serviceId);
        json.put("nsrsbh", nsrsbh);
        json.put("async", async);
        json.put("appid", appid);
        json.put("sid", sid);
        json.put("input", inpuMap);
        return json.toString();
    }

    /**
     * 余票查询
     *
     * @param appid
     * @param fplxdm
     * @return
     */
    public static String syncRemain(String appid, String nsrsbh, String fplxdm, String equipmentMode) {
        Map map = new HashMap();
        fplxdm = checkFplxdm(equipmentMode, fplxdm);
        map.put(FPLXDM, fplxdm);
        return getJson("HQDQFPDMHM", nsrsbh, TRUE, appid, UUIDUitl.UUID32(false), map);
    }

    public static String checkFplxdm(String mode, String fplxdm) {
        if (mode.equals("0")) {
            if (fplxdm.equals("004")) {
                fplxdm = "0";
            } else if (fplxdm.equals("007")) {
                fplxdm = "2";
            } else if (fplxdm.equals("026")) {
                fplxdm = "51";
            }
        }
        return fplxdm;
    }

    //根据发票类型代码匹配发票类型
    public static String convertInvoiceTypeName(String InvoiceType) {
        if ("004".equals(InvoiceType)) {
            return "增值税专用发票";
        } else if ("007".equals(InvoiceType)) {
            return "增值税普通发票";
        } else if ("026".equals(InvoiceType)) {
            return "增值税电子普通发票";
        }
        return "";
    }

    /**
     * 空白发票作废
     *
     * @param appid
     * @param fplxdm
     * @return
     */
    public static String cancelBlankInvoices(String appid, String nsrsbh, String fplxdm, String equipmentMode,
                                             String invoiceCode, String invoiceNo, String zfr) {
        Map map = new HashMap();
        fplxdm = checkFplxdm(equipmentMode, fplxdm);
        map.put(FPLXDM, fplxdm);
        if (CodeConstant.SKP.equals(equipmentMode)) {
            map.put("fpdm", "");
        }else {
            map.put("fpdm", invoiceCode);
        }
        map.put("fphm", invoiceNo);
        map.put("zfr", zfr);
        return getJson("KBFPZF", nsrsbh, TRUE, appid, UUIDUitl.UUID32(false), map);
    }

    /**
     * 应用登记
     *
     * @param devKey
     * @param sellerTaxNo
     * @return
     */
    public static String appRegister(String devKey, String sellerTaxNo) {
        JSONObject json = new JSONObject();
        json.put("serviceid", "CXAPP");
        json.put("dev_key", devKey);
        json.put("nsrsbh",sellerTaxNo);
        json.put("appid","");
        json.put("pageNumber","");
        json.put("pageSize","");
        return json.toString();
    }

    /**
     * 同步发票
     *
     * @param mode
     * @param appid
     * @param dateS
     * @param dateE
     * @param nsrsbh
     * @param invoiceType
     * @return
     */
    public static String syncInvoice(String mode, String appid, String dateS, String dateE, String nsrsbh, String invoiceType) {
        Map map = new HashMap();
        if (mode.equals("0")) {
            String year = dateS.substring(0, 4);
            String month = dateS.substring(4, 6);
            map.put(FPLXDM, "");
            map.put("cxfs", "1");
            map.put("year", year);
            map.put("month", month);
        } else {
            map.put(FPLXDM, invoiceType);
            map.put("cxfs", "1");
            map.put("cxtj", dateS + dateE);
            map.put("cxlx", "0");
        }
        return getJson("FPCX", nsrsbh, TRUE, appid, UUIDUitl.UUID32(false), map);

    }

    /**
     * 发票作废
     *
     * @param mode
     * @param appid
     * @param sellerInvoiceEntity
     * @return
     */
    public static String cancelInvoice(String mode, String appid, SellerInvoiceEntity sellerInvoiceEntity) {
        Map map = new HashMap();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        map.put("fpdm", sellerInvoiceEntity.getInvoiceCode());
        map.put("fphm", sellerInvoiceEntity.getInvoiceNo());
        map.put("hjje", sellerInvoiceEntity.getTotalSum());
        map.put("zfr", sellerInvoiceEntity.getDrawer());
        return getJson("FPZF", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid, UUIDUitl.UUID32(false), map);

    }
    
    public static String getTspz(String mode) {
        String tspz = "";
        if (mode.equals("0")) {
            tspz = "0";
        } else if (mode.equals("1")) {
            tspz = "00";
        }
        return tspz;
    }
    
    /**
     * 获取未使用发票代码号码
     *
     * @param mode
     * @param appid
     * @param sellerInvoiceEntity
     * @return
     */
    public static String getUserInvoiceNo(String mode, String appid, SellerInvoiceEntity sellerInvoiceEntity) {
        Map map = new HashMap();
        String fplxdm = checkFplxdm(mode, sellerInvoiceEntity.getInvoiceType());
        map.put(FPLXDM, fplxdm);
        return getJson("HQDQFPDMHM", sellerInvoiceEntity.getSellerTaxNo(), TRUE, appid, UUIDUitl.UUID32(false), map);
    }

    /**
     * 同步电票pdf下载地址
     *
     * @param appid
     * @param fps
     * @return
     */

    public static String updatePdfUrl(String appid,String sellerTaxNo,List<Map<String,String>> fps) {
        Map map = new HashMap();
        map.put("fp", fps);
        return getJson("DZFPBSWJCX", sellerTaxNo, TRUE, appid, UUIDUitl.UUID32(false), map);
    }

    public static String getKprq(String kprq) {
        return kprq.substring(0, 6);
    }
}
